package org.linlinjava.litemall.db.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.linlinjava.litemall.db.dao.LitemallBrandAppendMapper;
import org.linlinjava.litemall.db.dao.LitemallBrandMapper;
import org.linlinjava.litemall.db.domain.LitemallBrand;
import org.linlinjava.litemall.db.domain.LitemallBrand.Column;
import org.linlinjava.litemall.db.domain.LitemallBrandAppend;
import org.linlinjava.litemall.db.domain.LitemallBrandAppendExample;
import org.linlinjava.litemall.db.domain.LitemallBrandExample;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class LitemallBrandService {
    @Resource
    private LitemallBrandMapper brandMapper;
    @Resource
    private LitemallBrandAppendMapper appendMapper;
    private Column[] columns = new Column[]{Column.id, Column.name, Column.desc, Column.picUrl, Column.floorPrice};

    public List<LitemallBrand> query(Integer page, Integer limit, String sort, String order) {
        LitemallBrandExample example = new LitemallBrandExample();
        example.or().andDeletedEqualTo(false);
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }
        PageHelper.startPage(page, limit);
        return brandMapper.selectByExampleSelective(example, columns);
    }

    public List<LitemallBrand> query(Integer page, Integer limit) {
        return query(page, limit, null, null);
    }

    public LitemallBrand findById(Integer id) {
        return brandMapper.selectByPrimaryKey(id);
    }

    public List<LitemallBrand> querySelective(String id, String name, Integer page, Integer size, String sort, String order) {
        PageHelper.startPage(page, size);
        LitemallBrandExample example = new LitemallBrandExample();
        LitemallBrandExample.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(id)) {
            criteria.andIdEqualTo(Integer.valueOf(id));
        }
        if (!StringUtils.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        criteria.andDeletedEqualTo(false);

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }
        return brandMapper.selectByExample(example);
    }

    public int updateById(LitemallBrand brand) {
        brand.setUpdateTime(LocalDateTime.now());
        return brandMapper.updateByPrimaryKeySelective(brand);
    }

    public void deleteById(Integer id) {
        brandMapper.logicalDeleteByPrimaryKey(id);
    }

    public void add(LitemallBrand brand) {
        brand.setAddTime(LocalDateTime.now());
        brand.setUpdateTime(LocalDateTime.now());
        brandMapper.insertSelective(brand);
    }

    public List<LitemallBrand> all() {
        LitemallBrandExample example = new LitemallBrandExample();
        example.or().andDeletedEqualTo(false);
        return brandMapper.selectByExample(example);
    }

    public List<LitemallBrand> queryByUsername(String username) {
        LitemallBrandExample example = new LitemallBrandExample();
        example.or().andDeletedEqualTo(false).andUsernameEqualTo(username);
        return brandMapper.selectByExample(example);
    }

    public List<LitemallBrand> queryByDomain(String domain) {
        LitemallBrandExample example = new LitemallBrandExample();
        example.or().andDeletedEqualTo(false).andDomainEqualTo(domain);
        return brandMapper.selectByExample(example);
    }

    public  List<LitemallBrand> queryByName(String name) {
        LitemallBrandExample example = new LitemallBrandExample();
        example.or().andDeletedEqualTo(false).andNameEqualTo(name);
        return brandMapper.selectByExample(example);
    }
    public List<LitemallBrand> queryByCondition(Integer region,Integer pCategory, String condition, List<Integer> category, List<Integer> scale,
                                                List<Integer> qualification, String sort, String order, Integer page, Integer size) {
        condition = condition == null ? null : "%" + condition + "%";
        PageHelper.startPage(page, size);
        return brandMapper.queryByCondition(region, pCategory, condition, category, scale, qualification, sort, order);
    }

    public Integer getTotalNumByNum(Integer region,Integer pCategory, String condition, List<Integer> category, List<Integer> scale,
                                    List<Integer> qualification) {
        condition = condition == null ? null : "%" + condition + "%";
        return brandMapper.getTotalNumByNum(region, pCategory, condition, category, scale, qualification);
    }

    public List<LitemallBrand> findByUserName(String username) {
        LitemallBrandExample example = new LitemallBrandExample();
        example.or().andUsernameEqualTo(username).andDeletedEqualTo(false);
        return brandMapper.selectByExample(example);
    }

    public List<LitemallBrandAppend> getPicturesByBrandId(Integer brandId, String type) {
        LitemallBrandAppendExample example = new LitemallBrandAppendExample();
        example.or().andBrandIdEqualTo(brandId).andAppendTypeEqualTo(type);
        return appendMapper.selectByExample(example);
    }


    //显示公司等级A   B
    public PageInfo findBrandLevelPictureList(String level,Integer page,Integer limit){
        PageHelper.startPage(page,limit);

        if(StringUtils.isEmpty(level)){
            return null;
        }
        LitemallBrandExample example = new LitemallBrandExample();
        LitemallBrandExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);
        criteria.andBrandLevelEqualTo(level);
        List<LitemallBrand> litemallBrands = brandMapper.selectByExampleSelective(example);
        List<Map> picUrlList=new ArrayList<>();
        for (LitemallBrand litemallBrand : litemallBrands) {
            HashMap<String, String> urlNameMap = new HashMap<>();
            urlNameMap.put("picUrl",litemallBrand.getPicUrl());
            urlNameMap.put("brandName",litemallBrand.getName());
            urlNameMap.put("domain",litemallBrand.getDomain());
            urlNameMap.put("id",litemallBrand.getId().toString());
            urlNameMap.put("username",litemallBrand.getUsername());
            picUrlList.add(urlNameMap);
        }
        return new PageInfo(picUrlList);
    }

    /**
     * 倒序显示公司.6条  /newCompany
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @return
     */
    public List<LitemallBrand> findBrandList(Integer page, Integer limit, String sort, String order) {
        PageHelper.startPage(page, limit);
        LitemallBrandExample example = new LitemallBrandExample();
        LitemallBrandExample.Criteria criteria = example.createCriteria();

        criteria.andDeletedEqualTo(false);
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }
        return brandMapper.selectByExampleSelective(example);
    }

    public Page<LitemallBrand> findBrandAll( Integer page, Integer limit, String sort, String order) {
        PageHelper.startPage(page, limit);
        LitemallBrandExample example = new LitemallBrandExample();
        LitemallBrandExample.Criteria criteria = example.createCriteria();

        criteria.andDeletedEqualTo(false);
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }
        return (Page<LitemallBrand>)brandMapper.selectByExampleSelective(example);
    }

    /*BrandMapper...永辉方法。如果重写。Brand表容易覆盖。做备份
    List<LitemallBrand> queryByCondition(@Param(value = "region") Integer region, @Param(value = "pCategory") Integer pCategory,
                                         @Param(value = "condition") String condition, @Param(value = "category") List<Integer> category,
                                         @Param(value = "scale") List<Integer> scale, @Param(value = "qualification") List<Integer> qualification,
                                         @Param(value = "sort") String sort, @Param(value = "order") String order);

    Integer getTotalNumByNum(@Param(value = "region") Integer region, @Param(value = "pCategory") Integer pCategory,
                             @Param(value = "condition") String condition, @Param(value = "category") List<Integer> category,
                             @Param(value = "scale") List<Integer> scale, @Param(value = "qualification") List<Integer> qualification);
*/

    //显示 公司  推介recommend
    public Page<LitemallBrand> recommendCompany(Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        LitemallBrandExample example = new LitemallBrandExample();
        LitemallBrandExample.Criteria criteria = example.createCriteria();
        criteria.andRecommendEqualTo(true).andDeletedEqualTo(false);
        return (Page) brandMapper.selectByExampleSelective(example);
    }

    public List<Integer> brandIdList(){
     return   brandMapper.allBrandIdList();
    }

}
